package com.my.hotel.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.my.hotel.entity.*;
import com.my.hotel.service.*;
import com.my.hotel.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/kfRoom")
public class KfRoomController {

    @Autowired
    private KfRoomService kfRoomService;
    @Autowired
    private KfTypeService kfTypeService;

    @Autowired
    private KfImgService kfImgService;

    @Autowired
    private HdService hdService;
    @Autowired
    private EvaService evaService;
    @Autowired
    private UserService userService;
    @Autowired
    private EvaImgService evaImgService;

    //分页查询
    @RequestMapping("/kfRoomPage")
    public String getPage(Model model, Integer pageNo){
        if(pageNo==null){
            pageNo=1;
        }
        PageHelper.startPage(pageNo,10);
        List<KfRoom> list = kfRoomService.getList();
        PageInfo<KfRoom> page = new PageInfo<KfRoom>(list);
        model.addAttribute("page",page);
        return "kfRoomPage";
    }
    //客房信息预添加
    @RequestMapping("/addPrekfRoom")
    public String addPrekfRoom(Model model){
        //查询客房类别信息
        List<KfType> list = kfTypeService.getList();
        model.addAttribute("listType",list);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String now = sdf.format(date);
        List<Hd> listHd = hdService.getListByNow(now);
        model.addAttribute("listHd",listHd);
        return "kfRoomAdd";
    }
    //客房信息添加
    @RequestMapping("/addkfRoom")
    public String addkfRoom(KfRoom kfRoom,String fileImg,Model model){
        kfRoomService.addKfRoom(kfRoom,fileImg);
        return getPage(model);
    }
    //客房信息删除
    @RequestMapping("/delKfRoom")
    public String delKfRoom(int id,Model model){
        kfRoomService.deleteKfRoom(id);
        return getPage(model);
    }
    //客房信息预修改
    @RequestMapping("/updPreKfRoom")
    public String updPreKfRoom(int id,Model model){
        KfRoom kfRoom = kfRoomService.getKfRoomById(id);
        model.addAttribute("kfRoom",kfRoom);
        //查询客房类别信息
        List<KfType> list = kfTypeService.getList();
        model.addAttribute("listType",list);
        return "kfRoomUpdate";
    }
    //客房信息修改
    @RequestMapping("/updKfRoom")
    public String updKfRoom(KfRoom kfRoom,Model model){
        kfRoomService.updateKfRoom(kfRoom);
        return getPage(model);
    }

    //图片上传
    @RequestMapping("/fileUpload")
    @ResponseBody
    public String fileUpload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        InputStream is = file.getInputStream();
        Map<String, Object> map = OssUtil.upload(is, filename);
        String s = JSON.toJSONString(map);
        return s;
    }

    public String getPage(Model model){
        return getPage(model,1);
    }


    //房间预定
    @RequestMapping("/kfRoomyd")
    public String kfRoomyd(String kfTypeId,Model model, Integer pageNo){
        if(pageNo==null){
            pageNo=1;
        }
        //对客房图片进行全查询
        List<KfImg> kfImgs = kfImgService.selectAllKfImg();
        //对客类型进行全查询
        List<KfType> kfTypes = kfTypeService.getList();
        //对活动进行全查询
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
       // List<Hd> hds = hdService.getListByNow(format.format(new Date()));
        List<Hd> hds = hdService.getList();
        model.addAttribute("kfImgs",kfImgs);
        model.addAttribute("kfTypes",kfTypes);
        model.addAttribute("hds",hds);
        model.addAttribute("kfTypeId",kfTypeId);




        PageHelper.startPage(pageNo,5);

        if (kfTypeId==null){
            kfTypeId="";
        }
        List<KfRoom> list = kfRoomService.getListBykfTypeId(kfTypeId);
        PageInfo<KfRoom> page = new PageInfo<KfRoom>(list);
        List<KfRoom> list1 = page.getList();

        for (KfRoom kfRoom:list1){
            System.out.println(kfRoom);
        }
        model.addAttribute("page",page);

        //对点评进行全查询
        eva_all(model);

        return "forward:/index.jsp";
    }

    public void eva_all(Model model) {
        try {
            //对用户进行全查询
            List<User> userList = userService.getList(null);
            //对客房进行全查询
            List<KfRoom> kfRoomList = kfRoomService.getList();
            //对评论图片进行全查询
            List<EvaImg> evaImgList = evaImgService.evaImg_All();
            //查询好评数
            ArrayList<String> hp_list = new ArrayList<String>();
            hp_list.add("5");
            int hp_num = evaService.eva_hzc_num(hp_list);
            //查询好评数
            ArrayList<String> zp_list = new ArrayList<String>();
            zp_list.add("2");
            zp_list.add("3");
            zp_list.add("4");
            int zp_num = evaService.eva_hzc_num(zp_list);
            //查询好评数
            ArrayList<String> cp_list = new ArrayList<String>();
            cp_list.add("1");
            int cp_num = evaService.eva_hzc_num(cp_list);
            model.addAttribute("hp_num",hp_num);
            model.addAttribute("zp_num",zp_num);
            model.addAttribute("cp_num",cp_num);


            PageHelper.startPage(1,5);
            List<Eva> evas = evaService.eva_all();
            for (Eva eva:evas){
                String evaTime_str = eva.getEvaTime();
                SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
                Date evaTime = format1.parse(evaTime_str);
                String evaTime_str2 = format2.format(evaTime);
                eva.setEvaTime(evaTime_str2);
            }
            PageInfo<Eva> evaPageInfo = new PageInfo<Eva>(evas);
            model.addAttribute("userList",userList);
            model.addAttribute("kfRoomList",kfRoomList);
            model.addAttribute("evaImgList",evaImgList);
            model.addAttribute("evaPageInfo",evaPageInfo);
            model.addAttribute("pj","全部");
            model.addAttribute("px","时间排序");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }



}
